﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveCPEWinForm.Gestionnaire;
using LiveCPEWinForm.LiveCPEWS;

namespace LiveCPEWinForm.comp
{
    public partial class CsMessage : UserControl
    {
        private GestMessage lMessage;
        private GestAPP lGestAPP;
        public CsMessage(GestAPP pGestAPP, GestMessage pMessage)
        {
            InitializeComponent();
            lMessage = pMessage;
            lGestAPP = pGestAPP;
            // Chargment des messages
            InitMessage();
        }
        private void InitMessage()
        {
            string friendAccept = "[RequestAccepted]";
            string friendRequest = "[FriendshipRequest]";

            lblUser.Text = "(" + lMessage.Message.SenderName + ")";

            if (!(lMessage.Message.MessageContent.Contains(friendAccept)) && !(lMessage.Message.MessageContent.Contains(friendRequest)))
            {
                lblMessage.Text = lMessage.Message.MessageContent.Trim();
            }
            if (lMessage.Message.MessageContent.Contains(friendAccept))
            {
                lblMessage.Text = "Your friendship request has been accepted.";
            }
            if (lMessage.Message.MessageContent.Contains(friendRequest))
            {
                lblMessage.Text = "Friendship request. Please answer.";
                btnAccept.Visible = true;
                btnReject.Visible = true;
            }
            else
            {

                btnAccept.Visible = false;
                btnReject.Visible = false;
            }
            // Heure de publication
            lblHeurePost.Text = "Published on " + Convert.ToString(lMessage.Message.PublicationDate);
            lblNBLike.Text = Convert.ToString(lMessage.Message.NumLike);


            //gestion des images
            // Image
            foreach (UserWS user in lGestAPP.GestFriends.ListFriend)
            {
                if (user.UserName.ToLower() == lMessage.Message.SenderName.ToLower())
                {
                    try
                    {
                        if (user.Picture.Length > 0)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(user.Picture);
                            pbProfile.Image = System.Drawing.Image.FromStream(ms);
                        }
                    }
                    catch (Exception) { }
                }
            }
            foreach (UserWS user in lGestAPP.GestFriends.ListNoFriend)
            {
                if (user.UserName.ToLower() == lMessage.Message.SenderName.ToLower())
                {
                    try
                    {
                        if (user.Picture.Length > 0)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(user.Picture);
                            pbProfile.Image = System.Drawing.Image.FromStream(ms);
                        }
                    }
                    catch (Exception) { }
                }
            }
            // Gestion des commentaires
            int lTop = 160;
            foreach (CommentWS com in lMessage.Comments)
            {
                CsComment cs = new CsComment(lGestAPP, com);
                cs.Parent = this;
                // Augmentation de la hauteur
                this.Height += cs.Height + 20;
                cs.Left = 60;
                cs.Top = lTop;
                // Interespace
                lTop += cs.Height + 20;
                cs.Refresh();
            }

        }

        private void btnPubli_Click(object sender, EventArgs e)
        {
            // S'il y a du texte
            if (tbComment.Text.Length > 0)
            {
                lGestAPP.SendComment(Convert.ToString(lMessage.Message.MessageID), tbComment.Text);
                (sender as Button).Visible = false;
            }
        }

        private void lblLike_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            lGestAPP.LikeMessage(Convert.ToString(lMessage.Message.MessageID));
            (sender as LinkLabel).Visible = false;
        }

        private void CsMessage_Load(object sender, EventArgs e)
        {

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
             lblMessage.Text = "Result : "+ lGestAPP.AcceptFriendShip(Convert.ToString(lMessage.Message.MessageID));
             btnAccept.Visible = false;
             btnReject.Visible = false;
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            lGestAPP.RejectFriendShip(Convert.ToString(lMessage.Message.MessageID));
        }
    }
}
